<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/*
 * FIXME: Description goes here .. 
 * 
 * @package RedCross_Form
 * @category RedCross
 */
class RedCross_Form_Decorator_DateTime extends Zend_Form_Decorator_Abstract
{
	
	private $date_form_values_minutes = array();
	private $date_form_values_hours = array();
	private $date_form_values_days = array();
	private $date_form_values_months = array();
	private $date_form_values_years = array();
	
	public function render($content)
	{
		
		$element = $this->getElement();
		if(!$element instanceof RedCross_Form_Element_DateTime) //datetime
		{
			return $content;
		}
		
		$view = $element->getView();
		if(!$view instanceof Zend_View_Interface) 
		{
			return $content;
		}
		
		$minutes 	= $element->getMinutes();
		$hours 		= $element->getHours();
		$day 		= $element->getDay();
		$month 		= $element->getMonth();
		$year 		= $element->getYear();
		$name 		= $element->getFullyQualifiedName();
		
		$params = array(
			'size'		=> 2,
			'maxlength'	=> 2,
		);
		
		
		$yearParams = array(
			'size'		=> 4,
			'maxlength'	=> 4,
		);
		$date_minutes = $this->generateDateFormValuesMinutes();
		$date_hours = $this->generateDateFormValuesHours();
		$date_years = $this->generateDateFormValuesYears();
		$date_months = $this->generateDateFormValuesMonths();
		$date_days = $this->generateDateFormValuesDays();
		
		$markup =
					'<label class="event_add_datetime_group_item" for="' . $name . '[day]"> Dag: </label>' .	$view->formSelect($name . '[day]', 	$day, 	null, $date_days) 
					. '<label class="event_add_datetime_group_item" for="' . $name . '[month]"> M&#229;ned: </label>' . 	$view->formSelect($name . '[month]', 	$month, 	null, $date_months)
					. '<label class="event_add_datetime_group_item" for="' . $name . '[year]"> &#197;r: </label>' . 	$view->formSelect($name . '[year]', 	$year, 	null, $date_years)
					. '<label class="event_add_datetime_group_item" for="' . $name . '[hours]"> Klokken: </label>' . 	$view->formSelect($name . '[hours]', $hours,null, $date_hours)
					. '<label class="event_add_datetime_group_item" for="' . $name . '[minutes]"></label>' . 	$view->formSelect($name . '[minutes]',$minutes,null,$date_minutes);
				
		switch ($this->getPlacement()) 
		{
			case self::PREPEND:
				return $markup . $this->getSeparator() . $content;
			case self::APPEND:
			default:
				return '<li id="event_add_datetime_group">' . $content . $this->getSeparator() . $markup . '</li>';
		}
		
	}
	
	private function generateDateFormValuesMinutes(){
		
		for($i = 0; $i <= 55; $i+=5){
			if($i >= 10) {
				$date_minutes[$i] = $i;
			} else {
				$date_minutes[$i] = '0' . $i;
			}
		}
		return $date_minutes;
	}
	
	private function generateDateFormValuesHours(){
		
		for($i = 0; $i <= 23; $i++){
			if($i >= 10) {
				$date_hours[$i] = $i;
			} else {
				$date_hours[$i] = '0' . $i;
			}
			
		}
		return $date_hours;
	}
	
	private function generateDateFormValuesYears(){
		
		for($i = 2010; $i <= 2050; $i++){
			$date_years[$i] = $i;
		}
		return $date_years;
	}
	
	private function generateDateFormValuesMonths(){
		
		return array(
				'01' => 'Januar',
				'02' => 'Februar',
				'03' => 'Mars',
				'04' => 'April',
				'05' => 'Mai',
				'06' => 'Juni',
				'07' => 'Juli',
				'08' => 'August',
				'09' => 'September',
				'10' => 'Oktober',
				'11' => 'November',
				'12' => 'Desember');
	}
	
	private function generateDateFormValuesDays(){
		
		for($i = 1; $i <= 31; $i++){
			if($i >= 10) {
				$date_days[$i] = $i;
			} else {
				$date_days[$i] = '' . $i; /* Andreas: I removed the zero your added to single digit dates. - Rune */
			}
		}
		
		return $date_days;
	}
}